package com.loooooo.pm2024.modules.pm.service;


import com.loooooo.pm2024.exception.MyBaseException;
import com.loooooo.pm2024.modules.pm.mapper.WorkDayMapper;
import com.loooooo.pm2024.modules.pm.model.Task;
import com.loooooo.pm2024.modules.pm.model.WorkDay;
import com.loooooo.pm2024.modules.pm.model.WorkSummary;
import com.loooooo.pm2024.modules.pm.selector.WorkDaySelector;
import com.loooooo.pm2024.modules.pm.selector.WorkSummarySelector;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import com.loooooo.pm2024.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

@Service
public class WorkDayService {

    @Autowired
    WorkDayMapper workDayMapper;
    @Autowired
    IdService idService;
    @Autowired
    TransactionTemplate transactionTemplate;

    public WorkDay get(Long id) {
        return get(id, null);
    }


    public WorkDay get(Long id, WorkDaySelector selector) {
        WorkDay comment = workDayMapper.get(id);
        if (selector != null && comment != null) {

        }
        return comment;
    }

    public List<WorkDay> list(WorkDaySelector selector) {

        List<WorkDay> list = workDayMapper.select(selector);
        if (list.size() > 0) {

        }
        return list;
    }

    public int count(WorkDaySelector selector) {

        return workDayMapper.count(selector);
    }

    public int update(WorkDay workDay) {
        long now = System.currentTimeMillis();
        workDay.setUpdatedAt(now);
        Long userId = SecurityUtils.loginedUserId();
        workDay.setUpdatedBy(userId);

        if (workDay.getDate() != null) {
            workDay.setDate(DateUtils.localDate2Long(DateUtils.toLocalDateTime(workDay.getDate()).toLocalDate()));
            workDay.setDayOfWeek(DateUtils.toLocalDateTime(workDay.getDate()).getDayOfWeek().getValue());
        }

        if (workDay.getId() == null || workDay.isNewEntity() || workDay.getId() <= 0L) {
            if (userId == null) {
                MyBaseException.throwMe(10401, "未登录");
            }
            if (workDay.getId() == null || workDay.getId() <= 0L) {
                workDay.setId(idService.nextId());
            }
            if (workDay.getDate() == null) {
                return 0;
            }
            workDay.setCreatedAt(now);
            workDay.setCreatedBy(userId);
            return workDayMapper.insert(workDay);

        }

        return workDayMapper.update(workDay);
    }


}
